package com.weipeep.sys.controller;


import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weipeep.common.utils.IdGenerator;
import com.weipeep.sys.domain.ClassTypeDO;
import com.weipeep.sys.domain.UserDO;
import com.weipeep.sys.service.ClassTypeService;
import com.weipeep.sys.service.UserService;
import com.weipeep.sys.vo.StudentVo;
import com.weipeep.sys.vo.ValueTextVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.weipeep.sys.domain.StudentDO;
import com.weipeep.sys.service.StudentService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-15 10:23:55 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/student")
public class StudentController {
	@Autowired
	private StudentService studentService;
	@Autowired
	private UserService userService;
	@Autowired
	private ClassTypeService classTypeService;
	@GetMapping()
	@RequiresPermissions("sys:student:student")
	String Student(){
	    return "sys/student/student";
	}

	@GetMapping("/time")
	String time(){
		return "sys/student/time";
	}
	@GetMapping("/pictures")
	String pictures(){
		return "sys/student/pictures";
	}
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:student:student")
	public Result<Page<StudentVo>> list(Integer pageNumber, Integer pageSize, StudentVo student){
		// 查询列表数据
        Page<StudentVo> page = new Page<>(pageNumber, pageSize);
        
        page = studentService.selectStudentPage(page, student);
        int total = studentService.selectStudentCount(student);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("sys:student:add")
	String add(Model model){
		List<ValueTextVo> classTypeList =  classTypeService.fetchClassTypes();
		model.addAttribute("classTypeList",classTypeList);
	    return "sys/student/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:student:edit")
	String edit(@PathVariable("id") Long id,Model model){
		StudentDO student = studentService.getById(id);
		UserDO user = userService.getById(student.getUserId());
        List<ValueTextVo> classTypeList =  classTypeService.fetchClassTypes();
        model.addAttribute("classTypeList",classTypeList);
		model.addAttribute("student", student);
		model.addAttribute("user", user);
	    return "sys/student/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:student:add")
	public Result<String> save( StudentVo student){

		if(studentService.insertStudent(student)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:student:edit")
	public Result<String>  update( StudentVo student){
		studentService.updateStudent(student);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:student:remove")
	public Result<String>  remove( Long id){
		if(studentService.deleteStudentById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:student:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		studentService.deleteStudentBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
